//  Copyright © 2014 Allen County Public Library.

//        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.

package us.in.lib.acpl.Mobile_ACPL;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import com.google.analytics.tracking.android.EasyTracker;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.item.SearchResultTitle;
import us.in.lib.acpl.Mobile_ACPL.models.BestSellerTitle;
import us.in.lib.acpl.Mobile_ACPL.models.BestSellerTitlesResponse;
import us.in.lib.acpl.Mobile_ACPL.models.RecommendedReadingListsResponse;
import us.in.lib.acpl.Mobile_ACPL.models.RecommendedReadingTitle;
import us.in.lib.acpl.Mobile_ACPL.models.RecommendedReadingTitlesResponse;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseActivity;
import us.in.lib.acpl.Mobile_ACPL.rest.SirsiClient;
import us.in.lib.acpl.Mobile_ACPL.ws.BestSellerSearchHandler;
import us.in.lib.acpl.Mobile_ACPL.ws.SearchCatalogHandler;

@EActivity(R.layout.recommended_reading_list)
public class RecommendedReadingListActivity extends BaseActivity implements FeaturedTitleList{
	
	@RestService
	SirsiClient client;
	
	@ViewById
	ListView listView;
	
	
	List<RecommendedReadingTitle> titles;
	RecommendedReadingTitlesAdapter adapter;
	String listId;

	BestSellerSearchHandler handler;
	
	@AfterViews
	public void afterViews(){
		titles = new ArrayList<RecommendedReadingTitle>();
		adapter = new RecommendedReadingTitlesAdapter();
		listView.setAdapter(adapter);
		listId = getIntent().getStringExtra("listId");

		
		handler = new BestSellerSearchHandler(this);
		Mobile_ACPL app = (Mobile_ACPL) this.getApplication();
		app.setBestSellerSearchHandler(handler);
		
		getTitles();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}
	
	@Background
	public void getTitles(){
		RecommendedReadingTitlesResponse response = client.getRecommendedReadingTitles(listId);
		getTitlesComplete(response);
	}
	@UiThread
	public void getTitlesComplete(RecommendedReadingTitlesResponse response){
		ArrayList<RecommendedReadingTitle> newList = new ArrayList<RecommendedReadingTitle>();
		for(RecommendedReadingTitle title : response.recommendedReadingTitleInfo){
			boolean duplicate = false;
			for(RecommendedReadingTitle innerTitle : newList){
				if(innerTitle.title.equals(title.title)){
					duplicate=true;
					break;
				}
			}
			if(!duplicate){
				newList.add(title);
			}
		}
		
		
		
		this.titles=newList;
		adapter.notifyDataSetChanged();
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				RecommendedReadingTitle title = titles.get(position);
				
				String searchString = title.title+"+"+title.author;
				search(searchString);
				
				
			}
			
		});
		
	}
	
	public void handleSearchResults(List<SearchResultTitle> results){
		Intent intent = new Intent(this, SearchResultsActivity.class);
		intent.putExtra("source", "BestSellerListActivity");
		startActivity(intent);
	}
	
	public void search(String searchString) {



			EasyTracker.getTracker().sendEvent("Search", "RecommendedReadingList", null, null);


			handler.search(searchString,
					SearchCatalogHandler.RESULTS_PER_PAGE, "title",
					null, null);

		
	}
	
	public class RecommendedReadingTitlesAdapter extends BaseAdapter{
	

		
		@Override
		public int getCount() {

			return titles.size();
		}

		@Override
		public Object getItem(int position) {

			return titles.get(position);
		}

		@Override
		public long getItemId(int position) {
	
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			RecommendedReadingTitle title = titles.get(position);
			LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
			convertView = inflater.inflate(R.layout.recommended_reading_list_item, null);
			
			
			TextView tv = (TextView)convertView.findViewById(R.id.searchResultLine1);
			tv.setText(title.title);
			
			tv = (TextView)convertView.findViewById(R.id.searchResultLine2);
			tv.setText(title.author);
			
			convertView.findViewById(R.id.smallCover).setVisibility(View.VISIBLE);
			
			return convertView;
		}
		
	}
	
	
	

}
